package com.gking.centerSystem.service.AccessRejectionCollect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.AccessRejectionCollect;
import com.gking.centerSystem.mapper.AccessRejectionCollectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@Service("AccessRejectionCollectService")
public class AccessRejectionCollectServiceImpl extends ServiceImpl<AccessRejectionCollectMapper, AccessRejectionCollect> implements AccessRejectionCollectService {

    /**
     * 添加受控访问记录
     *
     * @param ipAddress ip地址
     */
    @Override
    public void setRejectionCollect(String ipAddress) {
        LocalDate currentDate = LocalDate.now();
        LocalTime currentTime = LocalTime.now();

        int currentHour = currentTime.getHour();
        String startPeriod = String.format("%02d:00", currentHour);
        String endPeriod = String.format("%02d:00", currentHour + 1);

        LambdaQueryWrapper<AccessRejectionCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessRejectionCollect::getIpAddress, ipAddress);
        queryWrapper.eq(AccessRejectionCollect::getDate, currentDate);
        queryWrapper.eq(AccessRejectionCollect::getStartPeriod, startPeriod);
        queryWrapper.eq(AccessRejectionCollect::getEndPeriod, endPeriod);

        List<AccessRejectionCollect> list = this.list(queryWrapper);
        try {
            if (!list.isEmpty()) {
                // 列表不为空，获取第一个元素
                AccessRejectionCollect accessRejectionCollect = list.get(0);
                accessRejectionCollect.setRejectionCount(accessRejectionCollect.getRejectionCount() + 1);
                this.updateById(accessRejectionCollect);
            } else {
                AccessRejectionCollect ac = new AccessRejectionCollect();
                ac.setIpAddress(ipAddress);
                ac.setDate(currentDate.toString());
                ac.setStartPeriod(startPeriod);
                ac.setEndPeriod(endPeriod);
                ac.setRejectionCount(1);
                this.save(ac);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取当前日期某个ip的受控访问记录
     *
     * @param ipAddress ip地址
     * @return i
     */
    @Override
    public Integer getDateRejectionCollect(String ipAddress) {
        LocalDate currentDate = LocalDate.now();

        LambdaQueryWrapper<AccessRejectionCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessRejectionCollect::getIpAddress, ipAddress);
        queryWrapper.eq(AccessRejectionCollect::getDate, currentDate);

        try {
            int total = 0;
            List<AccessRejectionCollect> list = this.list(queryWrapper);
            for (AccessRejectionCollect ac : list) total = total + ac.getRejectionCount();
            return total;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取当前日期现在时间段，某个ip的受控访问记录
     *
     * @param ipAddress ip地址
     * @return i
     */
    @Override
    public Integer getDatePeriodRejectionCollect(String ipAddress) {
        LocalDate currentDate = LocalDate.now();
        LocalTime currentTime = LocalTime.now();

        int currentHour = currentTime.getHour();
        String startPeriod = String.format("%02d:00", currentHour);
        String endPeriod = String.format("%02d:00", currentHour + 1);

        LambdaQueryWrapper<AccessRejectionCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessRejectionCollect::getIpAddress, ipAddress);
        queryWrapper.eq(AccessRejectionCollect::getDate, currentDate);
        queryWrapper.eq(AccessRejectionCollect::getStartPeriod, startPeriod);
        queryWrapper.eq(AccessRejectionCollect::getEndPeriod, endPeriod);

        try {
            List<AccessRejectionCollect> list = this.list(queryWrapper);
            AccessRejectionCollect accessRejectionCollect = list.get(0);
            return accessRejectionCollect.getRejectionCount();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 分页获取受控访问记录
     *
     * @param accessRejectionCollect 筛选条件
     * @param pageNum                分页页码
     * @param pageSize               分页大小
     * @return l
     */
    @Override
    public Result<Page> getRejectionCollect(AccessRejectionCollect accessRejectionCollect, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<AccessRejectionCollect> queryWrapper = new LambdaQueryWrapper<>();
        if (accessRejectionCollect.getIpAddress() != null)
            queryWrapper.like(AccessRejectionCollect::getIpAddress, accessRejectionCollect.getIpAddress());
        if (accessRejectionCollect.getDate() != null)
            queryWrapper.like(AccessRejectionCollect::getDate, accessRejectionCollect.getDate());
        queryWrapper.orderByDesc(AccessRejectionCollect::getCreateTime);

        try {
            Page<AccessRejectionCollect> pageInfo = new Page<>(pageNum, pageSize);
            Page<AccessRejectionCollect> collectPageData = this.page(pageInfo, queryWrapper);
            return Result.success("分页获取请求ip受控日志成功", collectPageData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("分页获取请求ip受控日志失败");
        }
    }
}
